#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
class Solution {
    int m, n;
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    bool as[51][51];
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        m = grid.size();
        n = grid[0].size();
        int a = 0;
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (!as[i][j] && grid[i][j] == 1)
                {
                    a = max(a, bfs(grid, i, j));
                }
            }
        }
        return a;
    }
    int bfs(vector<vector<int>>& grid, int i, int j)
    {
        int count = 0;
        queue<pair<int, int>> qw;
        qw.push({ i,j });
        count++;
        as[i][j] = true;
        while (qw.size())
        {
            auto [a, b] = qw.front();
            qw.pop();
            for (int i = 0; i < 4; i++)
            {
                int x = a + dx[i];
                int y = b + dy[i];
                if (x >= 0 && x < m && y >= 0 && y < n && !as[x][y] && grid[x][y] == 1)
                {
                    qw.push({ x,y });
                    count++;
                    as[x][y] = true;
                }
            }
        }
        return count;
    }
};